﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
//add compomnets
using System.Web.Script.Services;
using System.Web.Script.Serialization;
using Reniec.Business.Layer;
using Reniec.Entity.Layer;
using System.IO;
using System.Drawing;
using Reniec.Interface.Layer;

namespace Reniec.Services.Layer
{
    /// <summary>
    /// Summary description for Service1
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    [ScriptService]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class Persona_SL : System.Web.Services.WebService
    {
        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public string TestListPersons(string sNumero_Documento, string sHuella)
        {
            
            Persona_EL PrmPersona = new Persona_EL();
            PrmPersona.Str_numero_documento  = sNumero_Documento;
            PrmPersona.Txt_huella = (sHuella.ToString().Trim().Length == 0) ? null : sHuella.ToString().Trim().Replace("±","\\");
            if (PrmPersona.Txt_huella != null)
            {
                PrmPersona.Txt_huella = ImageToBase64(Image.FromFile(PrmPersona.Txt_huella), System.Drawing.Imaging.ImageFormat.Jpeg);
            //ImageToBase64
            }
            

            List<Persona_EL> ObjPerson = new Persona_BL().ListPerson(PrmPersona);
            return new JavaScriptSerializer().Serialize(ObjPerson);
        }

        //
        public string ImageToBase64(Image image,System.Drawing.Imaging.ImageFormat format)
        {
            using (MemoryStream ms = new MemoryStream())
            {
                // Convert Image to byte[]
                image.Save(ms, format);
                byte[] imageBytes = ms.ToArray();

                // Convert byte[] to Base64 String
                string base64String = Convert.ToBase64String(imageBytes);
                return "data:image/jpeg;base64," + base64String;
            }
        }
        
        public Image Base64ToImage(string base64String)
        {
            // Convert Base64 String to byte[]
            byte[] imageBytes = Convert.FromBase64String(base64String);
            MemoryStream ms = new MemoryStream(imageBytes, 0,
              imageBytes.Length);

            // Convert byte[] to Image
            ms.Write(imageBytes, 0, imageBytes.Length);
            Image image = Image.FromStream(ms, true);
            return image;
        }
        //
    }
}
